﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Projekt
{
    /// <summary>
    /// Uzletekkel kapcsolatos muveletek
    /// </summary>
    class Shop
    {
        Dalgen conn = Dalgen.Instance;

        /// <summary>
        /// uj uzlet hozzaadasa az adatbazishoz
        /// </summary>
        /// <param name="shopName">Uzlet neve</param>
        /// <param name="shopTelNumber">Uzlet tel. szama</param>
        /// <param name="shopAddress">Uzlet cime</param>
        /// <param name="ErrorMsg">Hiba uzenet</param>
        /// <returns>true ha sikerult, kulonben false</returns>
        public bool add(String shopName, String shopTelNumber, String shopAddress, String ErrorMsg)
        {
            try
            {
                String sqlQuery = "Insert into Uzletek(UNev,UCim,UTelSzam) values ('" + shopName + "','" + shopAddress + "','" + shopTelNumber + "')";
                return conn.nonQueryCmd(sqlQuery);
            }
            catch (Exception es) { throw (es); }
        }

        /// <summary>
        /// Lekeri az uzletek listajat(adatokkal)
        /// </summary>
        /// <returns>az adatbazisban szereplo uzletek listaja(datatable)</returns>
        public DataTable getShopList()
        {
            try
            {
                string sqlQuery = "Select * From Uzletek";
                return conn.QueryCmd(sqlQuery);
            }
            catch (Exception es) { throw (es); }
        }

        /// <summary>
        /// Lekeri az uzletek listajat(csak nev)
        /// </summary>
        /// <returns>az adatbazisban szereplo uzletek listaja(datatable)</returns>
        public DataTable getShopNameList()
        {
            try
            {
                string sqlQuery = "Select UNev, UzletID From Uzletek";
                return conn.QueryCmd(sqlQuery);
            }
            catch (Exception es) { throw (es); }
        }

        /// <summary>
        /// Uzlet torlese
        /// </summary>
        /// <param name="UzletNev">Uzlet neve</param>
        /// <returns>true ha sikerult, kulonben false</returns>
        public bool DeleteShop(String UzletNev)
        {
            try
            {
                string sqlQuery = "Delete from Uzletek where UNev = '" + UzletNev + "'";
                return (conn.nonQueryCmd(sqlQuery));
            }
            catch (Exception es) { /*throw (es);*/ return false; }
        }

        /// <summary>
        /// Kereses az uzletek kozott a parameterben kapott szurok alapjan
        /// </summary>
        /// <param name="shopName">Uzlet neve</param>
        /// <param name="shopAddress">Uzlet cime</param>
        /// <returns>a filtereknek megfelelo uzletek listaja(datatable)</returns>
        public DataTable SearchShop(String shopName, String shopAddress)
        {
            try
            {
                string sqlQuery = "Select * From Uzletek Where UNev like '" + shopName + "%' And UCim like '" + shopAddress + "%'";
                return conn.QueryCmd(sqlQuery);
            }
            catch (Exception es) { throw (es); }
        }

        /// <summary>
        /// Uzlet szerkesztese
        /// </summary>
        /// <param name="name">Uzlet neve</param>
        /// <param name="address">Uzlet cime</param>
        /// <param name="phone">Uzlet tel. szama</param>
        public void editShop(String name, String address, string phone){
            try
            {
                string sqlQuery = "Select UzletID From Uzletek Where UNev = '" + name + "'";
                int id = (int)conn.ScalarCmd(sqlQuery);
                sqlQuery = "Update Uzletek Set UNev = '" + name + "', UCim = '" + address + "', UTelSzam = '" + phone + "' where UzletID = '" + id + "'";
                conn.ScalarCmd(sqlQuery);
            }
            catch (Exception es) { throw es; }
        }
    }
}
